﻿Public Class XboxInfoRetrevalComp


#Region " Varaibles "

    Private m_Settings As XboxInfoRetrevalSettings

    Private m_Timer As New System.Timers.Timer(1000)
    Private m_LastUpdate As DMXIProxy.XboxInfo
    Private m_LatestValue As DMXIProxy.XboxInfo

#End Region

#Region " Properties "

    Friend Property LastUpdate() As DMXIProxy.XboxInfo
        Get
            Return m_LastUpdate
        End Get
        Private Set(ByVal value As DMXIProxy.XboxInfo)
            m_LastUpdate = value
        End Set
    End Property

    Friend Property LatestValue() As DMXIProxy.XboxInfo
        Get
            Return m_LatestValue
        End Get
        Private Set(ByVal value As DMXIProxy.XboxInfo)
            m_LatestValue = value
        End Set
    End Property

    Public ReadOnly Property TimeBetweenChecks() As TimeSpan
        Get
            Dim Span As TimeSpan = New TimeSpan(0, My.Settings.MinutesBetweenChecks, 0)
            If Span.Ticks > Double.MaxValue Or Span.Ticks < Double.MinValue Then
                Span = New TimeSpan(0, 10, 0)
            End If
            Return Span
        End Get
    End Property

    Public ReadOnly Property TimeSinceLastCheck() As TimeSpan
        Get
            Return LastTick.Subtract(LastCheck)
        End Get
    End Property

    Public ReadOnly Property TimeSinceLastUpdate() As TimeSpan
        Get
            Return TimeSinceLastCheck.Subtract(TimeBetweenChecks)
        End Get
    End Property

#End Region

#Region " Events & Delegates "

    Friend Delegate Sub XboxInfoRetreval_EventDelegate(ByVal source As XboxInfoRetrevalComp, ByVal e As XboxInfoRetrevalEventArgs)

    Friend Delegate Sub OnEventDelegate()

    Friend Event Update As XboxInfoRetreval_EventDelegate
    Friend Event Check As XboxInfoRetreval_EventDelegate
    Friend Event Tick As XboxInfoRetreval_EventDelegate

    Private Sub OnUpdate()
        'If View.InvokeRequired Then
        'View.Invoke(New OnEventDelegate(AddressOf OnUpdate), r)
        'Else
        RaiseEvent Update(Me, New XboxInfoRetrevalEventArgs(LastUpdate, LatestValue, LastCheck, LastTick, TimeBetweenChecks, TimeSinceLastCheck, TimeSinceLastUpdate))
        'End If
    End Sub

    Private Sub OnCheck()
        'If View.InvokeRequired Then
        'View.Invoke(New OnEventDelegate(AddressOf OnCheck), r)
        'Else
        RaiseEvent Check(Me, New XboxInfoRetrevalEventArgs(LastUpdate, LatestValue, LastCheck, LastTick, TimeBetweenChecks, TimeSinceLastCheck, TimeSinceLastUpdate))
        'End If
    End Sub


    Private Sub OnTick()
        'If View.InvokeRequired Then
        'View.Invoke(New OnEventDelegate(AddressOf OnTick))
        'Else
        RaiseEvent Tick(Me, New XboxInfoRetrevalEventArgs(LastUpdate, LatestValue, LastCheck, LastTick, TimeBetweenChecks, TimeSinceLastCheck, TimeSinceLastUpdate))
        'End If
    End Sub

#End Region

    Public Sub New()
        AddHandler m_Timer.Elapsed, AddressOf m_Timer_Elapsed
        m_Timer.Start()
    End Sub

    Public Sub UpdateSettings(ByVal Settings As XboxInfoRetrevalSettings)
        m_Settings = Settings
    End Sub

    Private Sub m_Timer_Elapsed()
        Debug.Write("m_Timer_Elapsed")
        m_Settings.LastTick = Now
        OnTick()
        Debug.Write("Should Check: " & (CheckValidSettings() And TimeSinceLastUpdate.Ticks > 0).ToString)
        If CheckValidSettings() And TimeSinceLastUpdate.Ticks > 0 Then
            Debug.Write("is Checking")
            m_Settings.LastCheck = Now
            CheckXboxLiveStatus()
        End If
    End Sub

    Friend Sub CheckXboxLiveStatus()
        Dim c As New DMXIProxy.XboxInfoSoapClient
        Debug.Write("GetXboxInfo")
        LatestValue = c.GetXboxInfo(m_GamerTag)
        OnCheck()
        If LastUpdate Is Nothing Then
            LastUpdate = LatestValue
            LastUpdateValue = LatestValue.ToPresenceString
            OnUpdate()
        ElseIf Not LastUpdateValue = LatestValue.ToPresenceString Then
            LastUpdate = LatestValue
            LastUpdateValue = LatestValue.ToPresenceString
            OnUpdate()
        End If
    End Sub

    Private Function CheckValidSettings() As Boolean
        If Not m_Settings Is Nothing AndAlso m_Settings.IsValid Then
            Return True
        End If
    End Function

End Class

Public Class XboxInfoRetrevalSettings

    Public Property GamerTag() As String
        Get
            Return My.Settings.GamerTag
        End Get
        Set(ByVal value As String)
            My.Settings.GamerTag = value
            My.Settings.Save()
        End Set
    End Property

    Public Property Enabled() As Boolean
        Get
            Return My.Settings.Enabled
        End Get
        Set(ByVal value As Boolean)
            My.Settings.Enabled = value
            My.Settings.Save()
        End Set
    End Property

    Public Property LastCheck() As Date
        Get
            Return My.Settings.LastCheck
        End Get
        Set(ByVal value As Date)
            My.Settings.LastCheck = value
            My.Settings.Save()
        End Set
    End Property

    Public Property LastTick() As Date
        Get
            Return My.Settings.LastTick
        End Get
        Set(ByVal value As Date)
            My.Settings.LastTick = value
            My.Settings.Save()
        End Set
    End Property

    Public Property LastUpdateValue() As String
        Get
            Return My.Settings.LastUpdateValue
        End Get
        Set(ByVal value As String)
            My.Settings.LastUpdateValue = value
            My.Settings.Save()
        End Set
    End Property

    Public Property MinutesBetweenChecks() As Integer
        Get
            Return My.Settings.MinutesBetweenChecks
        End Get
        Set(ByVal value As Integer)
            My.Settings.MinutesBetweenChecks = value
            My.Settings.Save()
        End Set
    End Property

    Public Property TwitterEmail() As String
        Get
            Return My.Settings.TwitterEmail
        End Get
        Set(ByVal value As String)
            My.Settings.TwitterEmail = value
            My.Settings.Save()
        End Set
    End Property

    Public Property TwitterPassword() As String
        Get
            Return My.Settings.TwitterPassword
        End Get
        Set(ByVal value As String)
            My.Settings.TwitterPassword = value
            My.Settings.Save()
        End Set
    End Property

    Public Property TwitterUsername() As String
        Get
            Return My.Settings.TwitterUsername
        End Get
        Set(ByVal value As String)
            My.Settings.TwitterUsername = value
            My.Settings.Save()
        End Set
    End Property

    Public Function IsValid() As Boolean
        Dim IsValidResult As Boolean = True
        If Me.GamerTag.Length < 3 Then IsValidResult = False
        If Me.TwitterEmail.Length = 0 Then IsValidResult = False
        If Me.TwitterPassword.Length = 0 Then IsValidResult = False
        If Me.TwitterUsername.Length = 0 Then IsValidResult = False
        Return IsValid
    End Function


End Class
